/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package gridworld_v04;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Jonah
 */
public class Bug extends Actor{
    /*implementation comment*/
    ////<editor-fold defaultstate="collapsed" desc="Static Variables">
    //</editor-fold>
    ////<editor-fold defaultstate="collapsed" desc="Instanced Variables">
    private List<Tile> safeTiles = new ArrayList();
    private int seed = 0;
    //</editor-fold>
    ////<editor-fold defaultstate="collapsed" desc="Constructor">
    public Bug(Tile currentTile, Game game, Board board, int lifespan, int hunger) {
        super(currentTile, game, board);
        super.setLifeSpan(lifespan);
        super.setNeedToeat(hunger);
    }
    //</editor-fold>
    ////<editor-fold desc="Class Methods">
    @Override public void checkSafeTiles() {
        //System.out.println("checkSafeTiles() called");
        safeTiles.clear();
        List<Tile> unsafeTiles = new ArrayList();
        //System.out.println("currentTile.getNeighbors()" + currentTile.getNeighbors());
        loop:
            for (Tile t : currentTile.getNeighbors()){
                for (Actor o: t.getOccupants() ){
                    if (o instanceof Critter){
                        unsafeTiles.addAll(t.getNeighbors());
                        //continue loop;
                    }else if(o instanceof Bug){
                        unsafeTiles.add(t);
                    }
                }
                safeTiles.add(t);
                //System.out.println("safe tile added");
            }
        //end loop:
        safeTiles.removeAll(unsafeTiles);
        if (safeTiles.size()<1){
            safeTiles.add(currentTile);
        }
    }
    private void checkFlowerTiles(){
        List<Tile> flowerTiles = new ArrayList();
        for(Tile t: safeTiles){
            for ( Actor o : t.getOccupants()){
                if (o instanceof Flower){
                    flowerTiles.add(t);
                }
            }
        }
        if (flowerTiles.size()>0){
            safeTiles = flowerTiles;
        }
    }
    
    @Override public void move() {
        Tile oldtile = currentTile;
        checkSafeTiles();
        if (lifeSpan < this.needsToEat){
            checkFlowerTiles();
        }
        moveToTile(safeTiles.get((int)(Math.random()*safeTiles.size())));
        updateLife();
        loop:// so that placing new flowers and placing new bugs don't interfere with eachother
        {
            if (seed > 0){
                if (seed == 1){
                    getGame().createActorInTile(TileEnum.Flower, oldtile);
                    break loop;
                }else
                seed--;
            }
            if (lifeSpan > getGame().bugReproduction){
                getGame().createActorInTile(TileEnum.Bug, oldtile);
                lifeSpan -= getGame().bugRproductionCost;
            }
        }
    }
    private void updateLife(){
        if (lifeSpan < needsToEat){
            for(Actor o : currentTile.getOccupants()){
                if (o instanceof Flower){
                    lifeSpan += super.getGame().bugEatsflower;//this value may change
                    if (Math.random() < getGame().chanceBugWillEatSeed){
                        seed = getGame().bugSeedTime;
                    }
                }
            }
        }
        lifeSpan--;
        if (lifeSpan < 1){
            System.out.println("bug:"+this+" has died");
            super.remove();
        } else {
            super.getGame().addActorReference(this);
        }
    }
    //</editor-fold>
    ////<editor-fold defaultstate="collapsed" desc="Interface Override Methods">
    //</editor-fold>

}
